//
//  MBSpaceMyParentChildShowTabbarView.m
//  MiBaoTu
//
//  Created by admin on 16/9/19.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceMyParentChildShowTabbarView.h"

@interface MBSpaceMyParentChildShowTabbarView () {
    NSInteger           selectIndex;
}

@property (nonatomic,strong)NSArray     *itemArr;
@property (nonatomic,strong)UIColor     *bgColor;
@property (nonatomic,strong)UIColor     *selectColor;

@end
@implementation MBSpaceMyParentChildShowTabbarView

- (id)initWithFrame:(CGRect)frame withCount:(NSArray *)items withbgColor:(UIColor *)bgColor withSelectColor:(UIColor *)selectColor
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.itemArr = items;
        self.bgColor = bgColor;
        self.selectColor = selectColor;
        
        [self onCreateUIWithCount:items withbgColor:bgColor withSelectColor:selectColor];
    }
    return self;
}

- (void)onCreateUIWithCount:(NSArray *)items withbgColor:(UIColor *)bgColor withSelectColor:(UIColor *)selectColor
{
    float itemWidth = 1.0 * SCREEN_WIDTH / items.count;
    for (int i=0; i<items.count; i++) {
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(itemWidth*i, 0, itemWidth-1, self.height-1);
        bt.backgroundColor = [UIColor whiteColor];
        bt.tag = 300+i;
        [bt setTitle:items[i] forState:UIControlStateNormal];
        [bt setTitle:items[i] forState:UIControlStateHighlighted];
        [bt setTitle:items[i] forState:UIControlStateSelected];
        [bt setTitleColor:bgColor forState:UIControlStateNormal];
        [bt setTitleColor:selectColor forState:UIControlStateHighlighted];
        [bt setTitleColor:selectColor forState:UIControlStateSelected];
        bt.titleLabel.font = [UIFont systemFontOfSize:14.f];
        if (ISIPAD) {
               bt.titleLabel.font = [UIFont systemFontOfSize:30*PERCEN];
        }
        [bt addTarget:self action:@selector(onSelectItemAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:bt];

        if (i == 0) {
            bt.selected = YES;
            selectIndex = bt.tag;
            
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, bt.bottom, itemWidth, 1)];
            lineView.backgroundColor = selectColor;
            lineView.tag = 299;
            [self addSubview:lineView];
        }
    }
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
     float itemWidth = 1.0 * SCREEN_WIDTH / self.itemArr.count;
    for (int i=0; i<self.itemArr.count; i++) {
        UIButton *bt = (UIButton *)[self viewWithTag:300+i];
        bt.frame = CGRectMake(itemWidth*i, 0, itemWidth-1, self.height-1);
        if (selectIndex == bt.tag) {
            UIView *lineView = (UIView *)[self viewWithTag:299];
            lineView.left = (bt.tag-300) * 1.0 * SCREEN_WIDTH / self.itemArr.count;
            lineView.width = itemWidth;
        }
    }
    [self setNeedsLayout];
}

- (void)onSelectItemAction:(UIButton *)bt
{
    if (selectIndex == bt.tag) {
        NSLog(@"同一个按钮重复点击！！");
        return;
    }
    bt.titleLabel.font=[UIFont systemFontOfSize:28*PERCEN];
   
    
    UIButton *oldBt = (UIButton *)[self viewWithTag:selectIndex];
    UIView *lineView = (UIView *)[self viewWithTag:299];
    
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:.35 animations:^{
        bt.selected = !bt.selected;
        oldBt.selected = !bt.selected;
        lineView.left = (bt.tag-300) * 1.0 * SCREEN_WIDTH / self.itemArr.count;
        selectIndex = bt.tag;
        
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(onSelectItemForView:withIndex:)]) {
            [weakSelf.delegate onSelectItemForView:weakSelf withIndex:(bt.tag-300)];
        }
    }];
}

- (void)onUPdateUIForSelectIndex:(NSInteger)index
{
    if (selectIndex == index+300) {
        NSLog(@"滑动到同一个:%d",(int)index);
        return;
    }
    
    UIButton *oldBt = (UIButton *)[self viewWithTag:selectIndex];
    UIButton *selectBt = (UIButton *)[self viewWithTag:(300+index)];
    UIView *lineView = (UIView *)[self viewWithTag:299];
    
    [UIView animateWithDuration:0 animations:^{
        selectBt.selected = !selectBt.selected;
        oldBt.selected = !selectBt.selected;
        lineView.left = (selectBt.tag-300) * 1.0 * SCREEN_WIDTH / self.itemArr.count;
        selectIndex = selectBt.tag;
        
    }];

}

- (void)onUpdateUIForTitle:(NSString *)title withIndex:(NSInteger)index
{
    if (index < self.itemArr.count) {
        UIButton *bt = (UIButton *)[self viewWithTag:(300+index)];
        [bt setTitle:title forState:UIControlStateNormal];
        [bt setTitle:title forState:UIControlStateSelected];
    }
}

@end
